/**
*	@file QueryThread.h
*
*	@brief this header defines class for query thread
*
*	@author li yuchen
*
*	@date 1/28/2011
*/

#ifndef QUERYTHREAD_H
#define QUERYTHREAD_H
#include <string>
#include <Windows.h>
#include <process.h>
#include <stdio.h>
using namespace std;

class QueryThread {
public:
	QueryThread();
	~QueryThread();

	int get_thread_num(){return this->thread_num_;};

	void Start();
	void QueryExecution();
	void Join();
	void Stop();

	void static GetLockAffectsLeft(){ EnterCriticalSection(&QueryThread::lock_affects_left);};
	void static ReleaseLockAffectsLeft(){ LeaveCriticalSection(&QueryThread::lock_affects_left);};
	void static GetLockAffectsRight(){ EnterCriticalSection(&QueryThread::lock_affects_right);};
	void static ReleaseLockAffectsRight(){ LeaveCriticalSection(&QueryThread::lock_affects_right);};
	void static GetLockNext(){ EnterCriticalSection(&QueryThread::lock_next);};
	void static ReleaseLockNext(){ LeaveCriticalSection(&QueryThread::lock_next);};
	void static GetLockNextBip(){ EnterCriticalSection(&QueryThread::lock_next_bip);};
	void static ReleaseLockNextBip(){ LeaveCriticalSection(&QueryThread::lock_next_bip);};


	void static InitLock(){ 
		InitializeCriticalSection(&lock_affects_left);
		InitializeCriticalSection(&lock_affects_right);
		InitializeCriticalSection(&lock_next); 
		InitializeCriticalSection(&lock_next_bip); 
	};
	void static DeleteLock(){ 
		DeleteCriticalSection(&lock_affects_left);
		DeleteCriticalSection(&lock_affects_right);
		DeleteCriticalSection(&lock_next);
		DeleteCriticalSection(&lock_next_bip); 
	};

	HANDLE get_handle(){return this->thread_handle_;};
	
private:

	/** handle to the thread object */
	HANDLE thread_handle_;
	/** a name to identify the thread */
	int thread_num_;

	static int total_thread_num_;
	static CRITICAL_SECTION lock_affects_left;
	static CRITICAL_SECTION lock_affects_right;
	static CRITICAL_SECTION lock_next;
	static CRITICAL_SECTION lock_next_bip;
};

#endif


